package com.joy.conf.mvc;

import com.google.common.collect.Lists;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.freemarker.FreeMarkerProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import java.util.List;
import java.util.Properties;

/**
 * @ClassName: WebMvcConfiguration
 * @Description: mvc配置
 * @Author: yjzhao
 * @Create: 2019-09-09  15:39
 **/
@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {
    /**
     * 国际化注册
     * @return
     */
    @Bean
    public LocaleResolver localeResolver() {
        return new SessionLocaleResolver();
    }

    /****
     * default page: redirect Url
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
		registry.addViewController("").setViewName("/main");
//		registry.addViewController("/").setViewName("/login");
    }

    @Autowired
    private FreeMarkerProperties properties;
    /***
     * 注册freemarker宏
     * @return
     * @throws Exception
     */
    @Bean
    public FreeMarkerConfigurer freeMarkerConfig() throws Exception {
        FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
        setFreemarkerProperties(configurer);
        freemarker.template.Configuration configuration = configurer.createConfiguration();

        configuration.setLocalizedLookup(false);
		//configuration.setTemplateExceptionHandler(null);

        List<String> includeList = Lists.newArrayList();
        includeList.add("macro/lib.ftl");
        configuration.setAutoIncludes(includeList);

        configurer.setConfiguration(configuration);

        return configurer;
    }

    /***
     * templateMethod register
     * @param configurer
     */
    private void setFreemarkerProperties(FreeMarkerConfigurer configurer) {
        configurer.setTemplateLoaderPaths(this.properties.getTemplateLoaderPath());
        configurer.setPreferFileSystemAccess(this.properties.isPreferFileSystemAccess());
        configurer.setDefaultEncoding(this.properties.getCharsetName());

        Properties settings = new Properties();
        settings.putAll(this.properties.getSettings());
        configurer.setFreemarkerSettings(settings);


        //Map<String, Object> variables = Maps.newConcurrentMap();

        //variables.put("formatDate", new FormatDate());
        //variables.put("upCnStr", new UpCnStr());

        //configurer.setFreemarkerVariables(variables);
    }

    @Getter
    @Setter
    @Value("${local.resource.dir}")
    private String localResourceDir;

    @Getter
    @Setter
    @Value("${local.resource.path}")
    private String localResourcePath;

    /**
     * @Description 注册 本地资源访问映射
     * @param registry
     * @return
     * @Author yjzhao
     * @Date 2019/9/20 11:19
     **/
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/" + this.getLocalResourcePath() + "/**")
                .addResourceLocations("file:" + this.getLocalResourceDir()+ "/");
        // 本地文件夹要以"flie:" 开头，文件夹要以"/" 结束，example：
        //registry.addResourceHandler("/abc/**").addResourceLocations("file:D:/pdf/");
    }


}
